package com.campus.utils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

public class PasswordMigration {
//    public static void main(String[] args) {
//        String rawPassword = "updatedPassword"; // 输入测试的明文密码
//        String storedPassword = "$2a$10$d6kEAUa4YOdhNy67/UQDHOXC7KyOFJ305UlRJtVriBEzPU08hMjci"; // 从数据库中取的加密密码
//
//        boolean matches = PasswordUtil.verifyPassword(rawPassword, storedPassword);
//        System.out.println("Password matches: " + matches);
//    }
//}


//

    public static void main(String[] args) {
        // 配置数据库连接
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/db_camps");
        dataSource.setUsername("root");
        dataSource.setPassword("");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        // 查询所有用户
        String selectSql = "SELECT id, password FROM users WHERE password LIKE '$2a$%'";
        String updateSql = "UPDATE users SET password = ? WHERE id = ?";

        // 固定明文密码
        String fixedPassword = "defaultPassword";

        jdbcTemplate.query(selectSql, (rs, rowNum) -> {
            int userId = rs.getInt("id");

            // 更新数据库为固定明文密码
            jdbcTemplate.update(updateSql, fixedPassword, userId);

            System.out.println("User ID " + userId + " password reset to default.");
            return null;
        });
    }
}
